from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import random as r
from selenium.webdriver.support.select import Select
import time as t
import os
from config import CREATE_DATA_PATH
from common.user_data import project_admin, focus_project_admin
from app_package import SERVER

# 增加控制鼠标输入的模块
from pymouse import PyMouse


url = 'http://cspub.teacheredu.cn'
if SERVER == 'study':
    url = 'http://study.teacheredu.cn'
elif SERVER == 'cs':
    url = 'http://cs.teacheredu.cn'

today = t.strftime('%Y-%m-%d', t.localtime(t.time()))


def input_rich_text(driver, frame_id, content):
    driver.switch_to.frame(driver.find_element(By.ID, frame_id))
    driver.find_element(By.XPATH, '//body').send_keys(content)
    driver.switch_to.default_content()


def input_read_only(driver, class_name, text):
    css = 'input[class*=\'%s\']' % class_name
    driver.execute_script('document.querySelector("%s").removeAttribute("readonly")' % css)
    driver.find_element(By.CSS_SELECTOR, css).send_keys(text)


def get_time(add_minute=0):
    return t.strftime('%H:%M:%S', t.localtime(t.time() + add_minute * 60))


def set_up_and_login(user):
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    driver.find_element(By.ID, r'username').send_keys(user[0])
    driver.find_element(By.ID, 'password').send_keys(user[1])
    ActionChains(driver).\
        drag_and_drop_by_offset(driver.find_element(By.XPATH, '//span[contains(@class,"dragBtn")]'), 200, 0).perform()
    driver.find_element(By.CLASS_NAME, 'login-find').click()
    # driver.find_element(By.LINK_TEXT, '自动化测试').click()

    driver.find_element(By.ID,"more_proj").click()  #更多项目
    driver.find_element(By.ID,'projectId').send_keys("12713")
    t.sleep(1)
    driver.find_element_by_link_text("查询").click()
    t.sleep(3)
    driver.find_element(By.LINK_TEXT, '去学习').click()

    driver.switch_to.window(driver.window_handles[1])
    return driver


def Xin_and_login(user):
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    driver.find_element(By.ID, r'username').send_keys(user[0])
    driver.find_element(By.ID, 'password').send_keys(user[1])
    ActionChains(driver).\
        drag_and_drop_by_offset(driver.find_element(By.XPATH, '//span[contains(@class,"dragBtn")]'), 200, 0).perform()
    driver.find_element(By.CLASS_NAME, 'login-find').click()
    # driver.find_element(By.LINK_TEXT, '自动化测试').click()

    driver.find_element(By.ID,"more_proj").click()  #更多项目
    driver.find_element(By.ID,'projectId').send_keys("14885")  #需要修改
    t.sleep(1)
    driver.find_element_by_link_text("查询").click()
    t.sleep(5)
    driver.find_element(By.LINK_TEXT, '去学习').click()

    driver.switch_to.window(driver.window_handles[1])
    return driver


def release_act(driver):
    driver.find_element(By.PARTIAL_LINK_TEXT, '活动').click()
    driver.find_element(By.LINK_TEXT, '发布').click()
    driver.find_element(By.CLASS_NAME, 'zdy').click()
    driver.find_element(By.ID, 'activityName').send_keys('App自动化测试活动%d' % (r.randint(100, 999)))
    Select(driver.find_element(By.ID, 'sel_sysDictId1')).select_by_visible_text('高中')
    Select(driver.find_element(By.ID, 'sel_sysDictId2')).select_by_visible_text('语文')
    driver.find_element(By.NAME, 'startTime').send_keys(today)
    driver.find_element(By.NAME, 'endTime').send_keys(str(int(today[:4])+1)+today[4:])
    input_rich_text(driver, 'ueditor_0', '这是活动的内容。。。')
    driver.find_element(By.LINK_TEXT, '保存到下一步').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤一')
    input_rich_text(driver, 'ueditor_0', '步骤一讨论')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a1').click()
    t.sleep(0.5)
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤二')
    input_rich_text(driver, 'ueditor_0', '步骤二问答')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a2').click()
    t.sleep(0.5)
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤三')
    input_rich_text(driver, 'ueditor_0', '步骤三资源下载')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a3').click()
    t.sleep(1)

    m = PyMouse()
    # 被限制使用权后，打开新的管理权限允许使用
    m.click(1112.50390625, 84.58203125, 1, 1)
    t.sleep(1.5)
    m.click(943.046875, 171.03515625, 1, 1)
    t.sleep(1.5)
    m.click(977.7890625, 347.41015625, 1, 1)
    t.sleep(1.5)
    act_window = driver.window_handles[1]
    driver.switch_to_window(act_window)

    # t.sleep(1.5)
    # # 点击不安全性，设置允许权限
    # m.click(151.41796875, 86.46875, 1, 1)
    # t.sleep(1)
    # m.click(356.03125, 257.69140625, 1, 1)
    # t.sleep(1)
    # m.click(338.72265625, 309.65234375,1 ,1)
    # t.sleep(1)
    # 上传附件，需要点击两次
    driver.find_element(By.ID, 'fileinput_copy_id').click()
    t.sleep(1.5)
    # driver.find_element(By.ID, 'fileinput_copy_id').click()  ---------
    # 多点击一次的问题？增加不同元素定位的方式
    #driver.find_element(By.CLASS_NAME, 'swfupload').click()
    # t.sleep(4)      -----------------
    # driver.find_element(By.ID, 'SWFUpload_0').click()

    # 使用upload.exe程序上传文件，在windows系统可以使用，mac系统不能使用
    # os.system(os.path.join(CREATE_DATA_PATH, 'upload.exe')) # 这个是调用系统执行程序，在mac不能执行
    # driver.find_element(By.ID, 'SWFUpload_0').send_keys(os.path.join(CREATE_DATA_PATH, '1.png'))
    # 通过鼠标点击选择文件并上传，在mac系统使用
    m.click(452.58984375, 144.9609375, 1, 1)
    # 选择的文件的鼠标位置，和选择打开的鼠标位置是可以随时改变的，可能是需要修改的
    t.sleep(0.5)
    m.click(996.671875, 595.62890625, 1, 1)
    t.sleep(10)

    driver.find_element(By.LINK_TEXT, '保存').click()
    t.sleep(2)
    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤四')
    input_rich_text(driver, 'ueditor_0', '步骤四资源分享')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a4').click()
    t.sleep(0.5)
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤五')
    input_rich_text(driver, 'ueditor_0', '步骤五辩论')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a5').click()
    t.sleep(0.5)
    # driver.find_element(By.ID,'edui33_iframeholder').click()
    input_rich_text(driver,'ueditor_1','这是正方的辩题。。。')
    input_rich_text(driver, 'ueditor_2', '这是反方的辩题。。。')
    # driver.find_element(By.ID, 'zhengfang' and 'ueditor_1').send_keys('这是正方的辩题。。。')
    # driver.find_element(By.ID, 'fanfang' and 'ueditor_2').send_keys('这是反方的辩题。。。')
    t.sleep(1)
    driver.find_element(By.LINK_TEXT, '保存').click()
    t.sleep(2)
    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤六')
    input_rich_text(driver, 'ueditor_0', '步骤六视频')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a6').click()
    t.sleep(0.5)
    driver.find_element(By.XPATH, '//input[@type="radio"][@value="0"]').click()
    t.sleep(0.5)
    driver.find_element(By.ID, 'shipinUrl').send_keys('https://www.bilibili.com/video/av34469435')
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤七')
    input_rich_text(driver, 'ueditor_0', '步骤七观课')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a7').click()
    Select(driver.find_element(By.ID, 'p_list')).select_by_index(1)
    t.sleep(3)
    Select(driver.find_element(By.ID, 'c_list')).select_by_index(1)
    t.sleep(0.2)
    driver.find_element(By.LINK_TEXT, '确认').click()
    t.sleep(5)
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤八')
    input_rich_text(driver, 'ueditor_0', '步骤八指定提交人')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a8').click()
    t.sleep(0.5)
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤九')
    input_rich_text(driver, 'ueditor_0', '步骤九成果展示')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a9').click()
    t.sleep(0.5)
    driver.find_element(By.ID, 'fileinput_copy_id').click()
    # driver.find_element(By.ID, 'SWFUpload_0').click()
    # 通过鼠标点击上传文件
    m.click(452.58984375, 144.9609375, 1, 1)
    t.sleep(0.5)
    m.click(996.671875, 595.62890625, 1, 1)

    t.sleep(1)
    # 这里是需要上查询文件的，需要准备具体的文件
    # os.system(os.path.join(CREATE_DATA_PATH, 'upload.exe')) # 调用系统工具执行
    t.sleep(1)
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤十')
    input_rich_text(driver, 'ueditor_0', '步骤十投票')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a10').click()
    t.sleep(0.5)
    driver.find_element(By.LINK_TEXT, '+新增选项').click()
    driver.find_element(By.ID, 'voteTitle').send_keys('这是投票的标题')
    driver.find_element(By.ID, 'voteOption0').send_keys('选项一')
    driver.find_element(By.ID, 'voteOption1').send_keys('选项二')
    driver.find_element(By.ID, 'voteOption2').send_keys('选项三')
    driver.find_element(By.LINK_TEXT, '保存').click()

    driver.find_element(By.LINK_TEXT, '新增环节').click()
    driver.find_element(By.ID, 'name').send_keys('步骤十一')
    input_rich_text(driver, 'ueditor_0', '步骤十一活动评价')
    driver.find_element(By.LINK_TEXT, '选择工具').click()
    driver.find_element(By.ID, 'a11').click()
    t.sleep(0.5)
    driver.find_element(By.LINK_TEXT, '保存').click()
    driver.find_element(By.LINK_TEXT, '发布').click()
    t.sleep(2)


def release_item(driver):
    driver.find_element(By.PARTIAL_LINK_TEXT, '试题').click()
    driver.find_element(By.LINK_TEXT, '发布').click()
    driver.find_element(By.ID, 'ajh').click()
    Select(driver.find_element(By.ID, 'p_list')).select_by_visible_text('语文计划')
    driver.find_element(By.ID, 'input_title').send_keys('App自动化测试客观题%d' % r.randint(100, 999))
    driver.execute_script('document.getElementById("endtime").removeAttribute("readonly");')
    driver.execute_script('document.getElementById("starttime").removeAttribute("readonly");')
    driver.find_element(By.ID, 'starttime').send_keys(today)
    driver.find_element_by_id('endtime').send_keys(str(int(today[:4])+1)+today[4:])
    driver.find_element(By.ID, 'input_title').click()
    driver.find_element(By.ID, 'checkbox').click()
    driver.find_element(By.NAME, 'random').send_keys('10')
    driver.find_element(By.LINK_TEXT, '下一步').click()
    driver.find_element(By.LINK_TEXT, '导入模板').click()
    driver.find_element(By.XPATH, '//input[@type="file"]').send_keys(os.path.join(CREATE_DATA_PATH, '丰都试卷八.xls'))
    driver.find_element(By.LINK_TEXT, '上传').click()
    driver.find_element(By.XPATH, '//a[@value="1"][text()="确定"]').click()
    driver.find_element(By.LINK_TEXT, '确认发布').click()
    t.sleep(2)


def release_questionnaire(driver):
    # driver.find_element(By.XPATH, '//a[contains(text(),"调查问卷")]').click()
    driver.find_element(By.LINK_TEXT,'调查问卷').click()
    driver.find_element(By.LINK_TEXT, '发调查问卷').click()
    t.sleep(5)
    driver.find_element(By.ID, 'all').click()
    driver.find_element(By.ID, 'input_title').send_keys('App自动化测试调查问卷%d' % r.randint(100, 999))
    driver.execute_script('document.getElementById("starttime").removeAttribute("readonly");')
    driver.find_element(By.ID, 'starttime').send_keys(today)
    driver.find_element(By.ID, 'input_title').click()
    driver.execute_script('document.getElementById("endtime").removeAttribute("readonly");')
    driver.find_element(By.ID, 'endtime').send_keys(str(int(today[:4])+1)+today[4:])
    driver.find_element(By.ID, 'input_title').click()
    driver.find_element(By.ID, 'checkbox').click()
    input_rich_text(driver, 'ueditor_0', '这是问卷的简介。具体详细信息等待你了解之后，就会明个该工具的使用规则。。这是问卷的简介。具体详细信息等待你了解之后，就会明个该工具的使用规则。。')
    driver.find_element(By.ID, 'next').click()
    driver.find_element(By.LINK_TEXT, '人工录入调查问卷').click()
    driver.find_element(By.LINK_TEXT, '判断题').click()
    driver.find_element(By.ID, 'editor1').send_keys('这是一道判断题')
    driver.find_element(By.LINK_TEXT, '保存').click()
    driver.find_element(By.LINK_TEXT, '单选题').click()
    driver.find_element(By.ID, 'editor2').send_keys('这是一道单选题')
    driver.find_element(By.ID, 'editor3').send_keys('选项1')
    driver.find_element(By.ID, 'editor4').send_keys('选项2')
    driver.find_element(By.LINK_TEXT, '保存').click()
    driver.find_element(By.LINK_TEXT, '多选题').click()
    driver.find_element(By.ID, 'editor5').send_keys('这是一道多选题')
    driver.find_element(By.ID, 'editor6').send_keys('选项1')
    driver.find_element(By.ID, 'editor7').send_keys('选项2')
    driver.find_element(By.LINK_TEXT, '保存').click()
    driver.find_element(By.LINK_TEXT, '填空题').click()
    driver.find_element(By.ID, 'editor8').send_keys('这是一道填空题')
    driver.find_element(By.LINK_TEXT, '保存').click()
    driver.find_element(By.LINK_TEXT, '问答题').click()
    driver.find_element(By.ID, 'editor9').send_keys('这是一道问答题')
    driver.find_element(By.LINK_TEXT, '保存').click()
    driver.find_element(By.LINK_TEXT, '预览已有试题').click()
    driver.find_element(By.LINK_TEXT, '确认发布').click()


def release_classes(project_name='面授自动化测试项目'):
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    driver.find_element(By.ID, 'username').send_keys(focus_project_admin[0])
    driver.find_element(By.ID, 'password').send_keys(focus_project_admin[1])
    ActionChains(driver).\
        drag_and_drop_by_offset(driver.find_element(By.XPATH, '//span[contains(@class,"dragBtn")]'), 200, 0).perform()
    driver.find_element(By.CLASS_NAME, 'login-find').click()
    driver.find_element(By.LINK_TEXT, "更多项目>").click()
    # driver.find_element(By.PARTIAL_LINK_TEXT, project_name).click()
    # driver.switch_to.window(driver.window_handles[1])
    driver.find_element(By.LINK_TEXT,"去学习").click()
    driver.find_element(By.LINK_TEXT, '考勤').click()
    driver.find_element(By.LINK_TEXT, '创建考勤').click()
    input_read_only(driver, 'startTime', today)
    input_read_only(driver, 'timeHour', "%s - %s" % (get_time(1), get_time(10)))
    driver.find_element(By.XPATH, '//*[@id="basicDataDiv"]/div[2]/div[1]/div[1]/input').click()
    t.sleep(0.5)
    driver.find_element(By.XPATH, "//div[@class='count-down']").click()
    t.sleep(0.5)
    driver.find_element(By.ID, "mytext").send_keys("北三环中路44号院")
    t.sleep(1)
    driver.find_element(By.ID, "mybtn").click()
    t.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="allmap"]/div[1]/div[2]/div[2]/span[1]').click()
    t.sleep(1)
    driver.find_element(By.XPATH, "//p[@class='sure'][text()='确定']").click()
    t.sleep(2)
    driver.quit()

def Ting(driver):
    driver.find_element(By.PARTIAL_LINK_TEXT, '听评课').click()
    driver.find_element(By.ID, 'upload_pop').click()  #发布听评课
    Select(driver.find_element(By.ID,'classvisitType')).select_by_value("1")
    driver.find_element(By.ID,"input_title").send_keys("听评课现场标题")
    Select(driver.find_element(By.ID, 'sel_sysDictId1')).select_by_value("8875")   #高中
    Select(driver.find_element(By.ID, 'sel_sysDictId2')).select_by_value("131917")  #综合实践
    driver.execute_script('document.getElementById("starttime").removeAttribute("readonly");')
    driver.find_element(By.ID, 'starttime').send_keys(today)
    driver.find_element(By.ID, 'input_title').click()
    Select(driver.find_element(By.ID,"starthour")).select_by_value("20")
    driver.find_element(By.ID, 'input_title').click()
    driver.execute_script('document.getElementById("endtime").removeAttribute("readonly");')
    driver.find_element(By.ID, 'endtime').send_keys(str(int(today[:4]) + 1) + today[4:])
    driver.find_element(By.ID, 'input_title').click()

    # input_read_only(driver, 'starttime', today)
    # input_read_only(driver, 'starthour', "%s - %s" % (get_time(1), get_time(10)))
    # input_read_only(driver, 'endtime', today+'1')
    driver.find_element(By.ID,'content').send_keys("这是听评课内容展示，需要不超过200字内容，我就下次多玩会hi欧委会恶化挥舞起航相互为其黑屋IQ和队伍吧对清兵线厚度为IQ回味起还带我去欧虎把武器")
    driver.find_element(By.PARTIAL_LINK_TEXT, '保存到下一步').click()
    driver.find_element(By.ID,'tmp_37').click()
    t.sleep(1)
    # driver.find_element(By.LINK_TEXT, '发布').click()
    driver.find_element(By.XPATH,'//*[@id="classvisit-content2"]/div[2]/a[1]').click()
    driver.find_element(By.PARTIAL_LINK_TEXT, '去查看').click()
    t.sleep(2)
    driver.quit()







if __name__ == '__main__':
    bs = set_up_and_login(project_admin)
    release_act(bs)
    # release_item(bs)
    # release_questionnaire(bs)
    bs.quit()
    # release_classes()   #面授
